<?php
require_once "base.php";

class User extends Base {

	var $userinfo = null;
	function User() {
		parent :: Base();
		$this->load->model('Model_user', 'model_user');
		if (isset ($_SESSION[FEUSERINFO])) {
			$this->userinfo = $_SESSION[FEUSERINFO];
		}
	}

	function index($node_id = 1) {
		$this->_nodeid = $node_id;
		return $this->render("home/index", null);
	}

	/**
	 *
	 *
	 */
	function login() {
		// Check post
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$this->load->helper(array (
				'form',
				'url'
			));
			$this->load->library('form_validation');
			$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

			$this->form_validation->set_rules('Username', 'Username', 'required');
			$this->form_validation->set_rules('Password', 'Password', 'required');

			// Check logged
			$username = $this->input->post("Username");
			$password = $this->input->post("Password");

			if ($this->form_validation->run() == FALSE) {
				return $this->render("home/login_form", array ());
			} else {
				// Load model
				$userinfo = $this->model_user->login($username, $password);

				if (!empty ($userinfo)) {
					$_SESSION[FEUSERINFO] = $userinfo;
					if (session_is_registered(FEUSERREFER)) {
						$refer = $_SESSION[FEUSERREFER];
						session_unregister(FEUSERREFER);
						redirect($refer, "refresh");
					} else {
						// Redirect to page info
						redirect(base_url(), "refresh");
					}
				} else {
					// Set error message
					$this->form_validation->set_message(array (
						'Username and Password is not match'
					));
					return $this->render("user/login", array ());
				}
			}
		} else {
			//redirect(base_url(), "refresh");
			return $this->render("user/login", array ());
		}
	}
	/**
	 *
	 *
	 */
	function logout() {
		// Remove session
		if (session_is_registered(FEUSERINFO)) {
			session_unregister(FEUSERINFO);
		}
		return redirect(base_url());
	}
	/**
	 *
	 *
	 */
	function forgotpasswd() {
		// Check post
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$this->load->library('form_validation');
			$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_useremail_check');
			if ($this->form_validation->run() === FALSE) {

			} else {
				$email = $this->input->post("email");
				// send email

				// render notice
				return $this->render("notice", array (
					"message" => "We has just sent email with a new password to your email."
				));
			}
		}
		return $this->render("user/forgotpasswd", array ());
	}
	/**
	 *
	 *
	 */
	function changepasswd() {
		// Check login
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$this->load->library('form_validation');
			$this->form_validation->set_rules('password_old', 'Password Old', 'trim|required|callback_passwd_check');
			$this->form_validation->set_rules('password_new', 'Password New', 'trim|required|');
			$this->form_validation->set_rules('password_conf', 'Password Confirm', 'trim|required|matches[password_new]');
			if ($this->form_validation->run() === FALSE) {

			} else {
				$passwd_new = $this->input->post("password_new");
				// send email with new password
				$this->model_user->changepasswd($this->userinfo['loginname'], $passwd_new);
				// render notice
				return $this->render("notice", array (
					"message" => "Change Password is successful"
				));
			}
		}
		return $this->render("user/changepasswd", array ());
	}
	/**
	 *
	 *
	 */
	function signup() {
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$this->load->library('form_validation');
			$this->form_validation->set_rules('item_loginname', 'Login Name', 'trim|required|callback_userloginame_check');
			$this->form_validation->set_rules('item_email', 'Email', 'trim|required|valid_email|callback_useremail_check');
			if ($this->form_validation->run() === FALSE) {

			} else {
				// validation
				$data = parse_input("item_", $_POST);
				// Check change passwd
				if (!empty ($data['passwd'])) {
					$data['passwd'] = buildPassword($data['passwd']);
				}
				unset ($data['passwd_conf']);
				$user_id = $this->model_user->insert_user($data);
				// Send email
				// Load notice screen
				return $this->render("notice", array (
					"message" => "Signup is successful"
				));
			}
		}
		return $this->render("user/signup", array ());
	}
	/**
	 *
	 *
	 */
	function modify() {
		// Check login
		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			// validation
			$data = parse_input("item_", $_POST);
			// Check change passwd
			if (!empty ($data['passwd'])) {
				$data['passwd'] = md5($data['passwd']);
			}
			unset ($data['id']);
			unset ($data['passwd']);
			unset ($data['passwd_conf']);
			$this->model_user->update_user($this->userinfo['id'], $data);
			$_SESSION[FEUSERINFO] = $this->model_user->find($this->userinfo['loginname']);
			
			return $this->render("notice", array (
				"message" => "Your information is updated successful"
			));
		}
		return $this->render("user/modify", array (
			"userinfo" => $this->userinfo
		));
	}

	/**
	 *
	 *
	 */
	function userloginame_check($str) {
		$id = $this->input->post("item_id");
		// Load email
		$isCheck = $this->model_user->check_loginname($str, $id);
		if (is_array($isCheck)) {
			// Email already exists! Please input another email!
			$message = "Login name already exists! Please input another login name!";
			$this->form_validation->set_message('userloginame_check', $message);
			return FALSE;
		} else {
			return TRUE;
		}
	}

	/**
	 *
	 *
	 */
	function passwd_check($str) {
		// Load email
		$passwd = $this->userinfo['passwd'];
		if ($passwd != buildPassword($str)) {
			// Email already exists! Please input another email!
			$message = "Password Old isn't match'!";
			$this->form_validation->set_message('passwd_check', $message);
			return FALSE;
		} else {
			return TRUE;
		}
	}

	/**
	 *
	 *
	 */
	function useremail_check($str) {
		$id = $this->input->post("item_id");
		// Load email
		$isCheck = $this->model_user->check_email($str, $id);
		if (is_array($isCheck)) {
			// Email already exists! Please input another email!
			$message = "Email already exists! Please input another email!";
			$this->form_validation->set_message('useremail_check', $message);
			return FALSE;
		} else {
			return TRUE;
		}
	}
}
?>